import api from "../../../api/api.js"
import consts from "../../../utils/consts.js"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    times: [],
    startTime: 9,
    endTime: 21,
    tableHeight: 100
  },
  // 上班时间
  bindStartWorkChange: function (e) {
    console.log(e.detail.value)
    console.log(this.data.startTime)
    var sel = Number(e.detail.value)
    if (sel > this.data.endTime) {
      this.setData({
        startTime: sel,
        endTime: sel
      })
    } else {
      this.setData({
        startTime: sel
      })
    }
    this.upUserConf()
  },
  // 下班时间
  bindEndWorkChange: function (e) {
    var sel = Number(e.detail.value)
    if (sel < this.data.startTime) {
      this.setData({
        endTime: this.data.startTime
      })
    } else {
      this.setData({
        endTime: sel
      })
    }
    this.upUserConf()
  },
  // 表格高度
  bindTableHeightChange: function (e) {
    this.setData({
      tableHeight: e.detail.value
    })
    this.upUserConf()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      times: consts.times,
      startTime: getApp().globalData.userConf.startTime,
      endTime: getApp().globalData.userConf.endTime,
      tableHeight: getApp().globalData.userConf.tableHeight
    })
    console.log(this.data)
  },

  upUserConf: function () {
    var that = this
    api.upUserConf({
      method: 'get',
      data: {
        openid: getApp().globalData.openid,
        startTime: this.data.startTime,
        endTime: this.data.endTime,
        tableHeight: this.data.tableHeight
      },
      success: function (resp) {
        if (resp.code == 0) {
          getApp().globalData.userConf = resp.data
          that.setData({
            startTime: resp.data.startTime,
            endTime: resp.data.endTime,
            tableHeight: resp.data.tableHeight
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})